package com.okaysoft.ofb.account

import org.springframework.dao.DataIntegrityViolationException
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import grails.converters.JSON

class ModuleController {

    def springSecurityService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
       // params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        //[moduleInstanceList: Module.list(params), moduleInstanceTotal: Module.count()]
    }

    def listJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page'):0
        params.offset = (params.page - 1)* params.max
        params.sort = "orderNumber"
        params.order = "asc"

        def modules = Module.list(params)
        def count =  Module.count()
        render([total:count,rows:modules]as JSON)
    }

    def save() {

        def moduleInstance = Module.get(params.id)
        if (!moduleInstance) {
            moduleInstance = new Module(params)
        }

        moduleInstance.properties = params

        if (!moduleInstance.save(flush: true)) {
            render([status: 0,msg:message(code: 'default.save.failure.message', default: 'Save failure')] as JSON)
            return
        }

        render([status: 1,msg:message(code: 'default.save.success.message', default: 'Save success')] as JSON)
    }

    def edit() {
        def moduleInstance = Module.get(params.id)
        if (!moduleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'module.label', default: 'Module'), params.id])
            redirect(action: "list")
            return
        }

        [moduleInstance: moduleInstance]
    }

    def delete() {
        def moduleInstance = Module.get(params.id)
        if (!moduleInstance) {
            render([status: '0',msg: message(code: 'default.not.found.message', args: [message(code: 'module.label', default: 'Module'), params.id])] as JSON)
            return
        }

        try {
            moduleInstance.delete(flush: true)
            render([status: '1',msg:message(code: 'default.deleted.message', args: [message(code: 'module.label', default: 'Module'), params.id])] as JSON)
            return
        }
        catch (DataIntegrityViolationException e) {
            render([status: '0',msg:message(code: 'default.not.deleted.message', args: [message(code: 'module.label', default: 'Module'), params.id])] as JSON)
            return
        }
    }
    
    def tabs() {
        render("<iframe style=\"width:100%;height:99%;\" frameborder=0  border=0  marginwidth=0  marginheight=0 src=\"${createLink(uri: params.link)}\"></iframe>")
    }

    def menus() {
        params.sort = "orderNumber"
        params.order = "asc"
        def principal = springSecurityService.principal
        def authorities = principal.authorities
        def authLen = authorities.size()
        /*def modules = Module.list(params)
        def ms = []
        modules.each {m ->
            def requestMaps = m.modules
            requestMaps = requestMaps.sort {e1, e2 ->
                return e1.orderNumber - e2.orderNumber
            }
            def nrms = []
            requestMaps.each {rm ->
                def menuEnabled = rm.menuEnabled
                if(!menuEnabled)  {
                    return
                }
                def cas = rm.configAttribute.tokenize(',')
                def totLen = cas.size() + authLen
                cas.addAll(authorities)
                cas.unique()
                if (totLen != cas.size()) {
                    nrms << [id: rm.id, name: rm.name, url: rm.url, configAttribute: rm.configAttribute, link: rm.link]
                }
            }
            if (nrms) {
                ms << [name: m.name, id: m.id, modules: nrms]
            }
        }*/
        def rms = Requestmap.findAllByMenuEnabled(Boolean.TRUE,params)
        def nrms = []
        rms.each {rm ->

            def cas = rm.configAttribute.tokenize(',')
            def totLen = cas.size() + authLen
            cas.addAll(authorities)
            cas.unique()
            if (totLen != cas.size()) {
                nrms << [id: rm.id, name: rm.name, url: rm.url, configAttribute: rm.configAttribute, link: rm.link]
            }
        }
        //render([modules: ms] as JSON)
        [modules: nrms]
        //return
    }
}
